package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.generator.controller.vo.EnterpriseSupervisionBackPaySaveVo;
import io.renren.modules.generator.dao.EnterpriseMainDao;
import io.renren.modules.generator.dao.EnterpriseSupervisionBackPayRecordDao;
import io.renren.modules.generator.entity.EnterpriseSupervisionBackPayRecordEntity;
import io.renren.modules.generator.service.EnterpriseSupervisionBackPayRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;


/**
 * @author zhengd
 */
@Slf4j
@Service("enterpriseSupervisionBackPayRecordService")
public class EnterpriseSupervisionBackPayRecordServiceImpl extends ServiceImpl<EnterpriseSupervisionBackPayRecordDao, EnterpriseSupervisionBackPayRecordEntity> implements EnterpriseSupervisionBackPayRecordService {

    @Autowired
    private EnterpriseMainDao enterpriseMainDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,String keyword, String deptId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("enterprise_name",keyword);
        }
        if(StringUtils.isNotBlank(deptId)){
            List<String> list = enterpriseMainDao.getEnterpriseList(deptId, null);
            if (!CollectionUtils.isEmpty(list)) {
                queryWrapper.in("enterprise_code", list);
            }
        }
        queryWrapper.orderByDesc("create_time","id");
        IPage<EnterpriseSupervisionBackPayRecordEntity> page = this.page(
                new Query<EnterpriseSupervisionBackPayRecordEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBackPayEntity(EnterpriseSupervisionBackPaySaveVo vo) {
        EnterpriseSupervisionBackPayRecordEntity enterpriseSupervisionBackPayRecord = new EnterpriseSupervisionBackPayRecordEntity();
        enterpriseSupervisionBackPayRecord.setEnterpriseCode(vo.getCode());
        enterpriseSupervisionBackPayRecord.setEnterpriseName(vo.getName());
        enterpriseSupervisionBackPayRecord.setBackPayDesc(vo.getDesc());
        enterpriseSupervisionBackPayRecord.setCreateUserId(vo.getUserNo());
        enterpriseSupervisionBackPayRecord.setCreateUserName(vo.getUserName());
        enterpriseSupervisionBackPayRecord.setPic(vo.getPics());
        save(enterpriseSupervisionBackPayRecord);
    }
}
